<template>
  <el-dialog title="修改密码" :visible.sync="show" append-to-body width="480px">
    <el-form :model="form" ref="formValidate" :rules="rules">
      <el-form-item label="新密码" prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入" size="small"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPwd">
        <el-input v-model="form.confirmPwd" type="password" placeholder="请输入" size="small"></el-input>
      </el-form-item>
    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="show = false" size="small">取 消</el-button>
        <el-button type="primary" @click="confirmFun" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script lang="ts">
import { Component, Vue, PropSync, Ref ,Watch} from "vue-property-decorator";
interface Form {
    password: string,
    confirmPwd: string
}
@Component
export default class UpdatePassword extends Vue {
  @PropSync("isShow") show!: boolean;
  @Ref () formValidate!: HTMLFormElement
  @Watch('show')
  watchShow(val){
    if(val){
      this.form = {
        password: '',
        confirmPwd: ''
      }
      this.formValidate && this.formValidate.clearValidate()
    }
  }
  form:Form = {
    password: '',
    confirmPwd: ''
  }
  validateConfirmity = (rule,value,callback)=>{
    if(value && value !== this.form.password){
      callback(new Error('两次输入的密码不一致'))
    }else{
      callback()
    }
  }
  rules:any = {
    password: this.$formValidator.empty('新密码'),
    confirmPwd: [this.$formValidator.empty('确认密码'),
      {
        validator:this.validateConfirmity,trigger:'blur'
      }
    ],
  }

  confirmFun () {
    this.formValidate.validate((valid: boolean) => {
        if(valid) {
          this.$api.common.changePsdApi(this.form).then(res=>{
            this.show = false;
          })
        }
    })
  }
}
</script>

<style lang="scss" scoped>
</style>